using UnityEngine;
using UnityEngine.UI;

public class ChooseStairPanel : BasePanel
{
    [Header("关闭按钮")]
    public Button closeButton;
    [Header("格子")]
    public Transform parent;
    public GameObject slot;
    public override void OnClose()
    {
        base.OnClose();
    }

    public override void OnInit()
    {
        base.OnInit();

        path="UI/Panel/ChooseStairPanel";
        type=PanelType.Panel;

        GameManager.Instance.canOprate=false;
    }

    public override void OnShow(params object[] par)
    {
        base.OnShow(par);
        closeButton=item.transform.Find("Close").GetComponent<Button>();
        parent=item.transform.Find("Scroll View/Viewport/Content");
        slot=parent.Find("Slot").gameObject;

        slot.SetActive(false);

        closeButton.onClick.AddListener(() => { CloseButton(false); });
        CreateSlot();
    }

    //关闭
    public void CloseButton(bool isJump)
    {
        Close();
        GameManager.Instance.canOprate=!isJump;

        AudManager.instance.PlayUIAud(AudManager.instance.uiDic["点击"], 1);
    }
    //创建格子
    public void CreateSlot()
    {
        for(int i=1;i<=60;i++)
        {
            //if (i==1||i%5==0)
            //{
            //}
            GameObject newSlot = Instantiate(slot);
            newSlot.SetActive(true);
            newSlot.transform.SetParent(parent, false);
            newSlot.name=i.ToString();
            newSlot.transform.Find("Text").GetComponent<Text>().text=i.ToString();
            if(GameManager.Instance.playerDieData==null||GameManager.Instance.playerDieData.isDie==false||GameManager.Instance.playerDieData.levelId!=i)
            {
                newSlot.transform.Find("DiePlayer").gameObject.SetActive(false);
            }
            if (GameManager.Instance.unLockLevel[i-1])
            {
                newSlot.transform.Find("Lock").gameObject.SetActive(false);
            }
            newSlot.GetComponent<Button>().onClick.AddListener(() =>
            {
                SlotEvent(int.Parse(newSlot.name));
            });
        }
    }
    //格子事件
    public void SlotEvent(int id)
    {
        AudManager.instance.PlayUIAud(AudManager.instance.uiDic["点击"], 1);

        if (!GameManager.Instance.unLockLevel[id-1])
        {
            TipSayManager.Instance.Say("此楼层<color=red> 无法到达 </color>");

            return;
        }

        PanelManager.Open<BlackPanel>(true, true,id,false,0);

        CloseButton(true);
    }
}
